package com.lx.javasunmmrize.SingletonDemo;

/**
 * @Description 线程安全，双重检查double check
 * <p>
 * 优点：可以实现懒加载，线程安全，建议使用
 * 缺点：
 * @Author lx
 * @Date 2021/2/23 22:12
 **/
public class SingletonTest05 {

    public static void main(String[] args) {
        Singleton05 instanc = Singleton05.getInstance();
        Singleton05 instanc2 = Singleton05.getInstance();
        System.out.println(instanc == instanc2);
        System.out.println(instanc.hashCode());
        System.out.println(instanc2.hashCode());
    }
}

class Singleton05 {
    private static volatile Singleton05 instance;

    private Singleton05() {
    }

    public static Singleton05 getInstance() {
        if (instance == null) {
            synchronized (Singleton05.class){
                if (instance==null){
                    instance = new Singleton05();
                }
            }

        }
        return instance;
    }

}